//https://codepen.io/Xanmia/pen/DoljI
export default class explodeAnimation{
  constructor(x, y, THREE, scene){
    let movementSpeed = 0.1;
    this.totalObjects = 100;
    let objectSize = 0.1;
    let colors = [0xFF0FFF, 0xCCFF00, 0xFF000F, 0x996600, 0x000000];
    this.dirs = [];
    let geometry = new THREE.Geometry()
    for (let i = 0; i < this.totalObjects; i ++) { 
      let vertex = new THREE.Vector3();
      vertex.x = x;
      vertex.y = y;
      vertex.z = 0;
    
      geometry.vertices.push( vertex );
      this.dirs.push({x:(Math.random() * movementSpeed)-(movementSpeed/2),y:(Math.random() * movementSpeed)-(movementSpeed/2),z:(Math.random() * movementSpeed / 2)});
    }
    let material = new THREE.PointsMaterial( { size: objectSize,  color: colors[Math.round(Math.random() * colors.length)] });
    let particles = new THREE.Points( geometry, material );
    
    this.object = particles;
    this.status = true;
    
    this.xDir = (Math.random() * movementSpeed)-(movementSpeed/2);
    this.yDir = (Math.random() * movementSpeed)-(movementSpeed/2);
    this.zDir = (Math.random() * movementSpeed)-(movementSpeed/2);
    scene.add( this.object); 
  }
  update(){
    if (this.status == true){
      var pCount = this.totalObjects;
      while(pCount--) {
        var particle =  this.object.geometry.vertices[pCount]
        particle.y += this.dirs[pCount].y;
        particle.x += this.dirs[pCount].x;
        particle.z += this.dirs[pCount].z;
      }
      this.object.geometry.verticesNeedUpdate = true;
    }
  }
}